package com.cjjd.c.feature.mine.information;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.BaseSwipeRefreshFragment;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.AddressListBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ListResult;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.FragmentUtil;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/8/16.
 */

public class AddressFragment extends BaseSwipeRefreshFragment {
    @Override
    public Object getTitleString() {
        return null;
    }


    @Override
    public void responseSuccess(Call<HttpResult> call, Response<HttpResult> response) {
        ListResult<AddressListBean> bean = (ListResult<AddressListBean>) response.body().getData();
        setDataList(bean.dataList);
    }

    @Override
    protected void initView() {
        listBase.setDivider(getResources().getDrawable(R.color.divider_line_color));
        listBase.setDividerHeight(1);
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.item_header_information, listBase, false);
        TextView tvName = (TextView) headerView.findViewById(R.id.tv_name);
        tvName.setText("新增常用地址");

        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                FragmentUtil.replaceWithAddToBack(mContext, new AddressAddFragment(), R.id.fragment_container);
            AddressAddActivity.lauch(mContext);
            }
        });
        listBase.addHeaderView(headerView);
        listBase.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("xyd", "potion is " + position);
                if (dataList != null) {
                    AddressListBean itemData = (AddressListBean) dataList.get(position - 1);
                    AddressInforFragment addressInforFragment = new AddressInforFragment();
                    addressInforFragment.setUuid(itemData.uuid);
                    FragmentUtil.replaceWithAddToBack(mContext, addressInforFragment, R.id.fragment_container);
                }
            }
        });
        super.initView();
    }

    @Override
    public MyBaseAdapter getAdapter() {
        return new InformationAdapter(mContext, InformationAdapter.ADDRESS);
    }

    @Override
    public Call<HttpResult> getCall() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("nowPage", PAGE + "");
        h1.put("pageShow", PAGE_SIZE + "");
        return service.getCustomerAddressList(JsonUtils.convertParam("getCustomerAddressList", h1));
    }
}
